//
//  WeatherView.m
//  QuickCure
//
//  Created by Kent on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "WeatherView.h"
#import "ProvinceModel.h"
#import "SSZProvinceViewController.h"
#import "WeatherModel.h"
#import <UIImageView+WebCache.h>

@interface WeatherView () <UIScrollViewDelegate>

@property (nonatomic,strong) NSArray *provinceList;

@property (nonatomic,weak) UIImageView *lineImgView;

#pragma mark --- 基本资料
/**
 *  地点名
 */
@property (nonatomic,weak) UILabel *cityNameLabel;

#pragma mark --- 关于布局的成员属性
/**
 *  日期 + 地点
 */
@property (nonatomic,weak) UILabel *currentDateCity;

/**
 *  温度 + 空气质量
 */
@property (nonatomic,weak) UILabel *temperatureAirQuality;

/**
 *  天气状况
 */
@property (nonatomic,weak) UILabel *weatherLabel;

/**
 *  天气图标
 */
@property (nonatomic,weak) UIImageView *weatherIconView;

// ======================================================================================
/**
 *  定时器
 */
@property (nonatomic, strong) CADisplayLink *link;

//scrollview的contentoffset的X
@property (nonatomic, assign) CGFloat offsetX;

@property (nonatomic,weak) UIScrollView *scrollView;

@property (nonatomic,strong) NSMutableArray *scrollArray;

@property (nonatomic,weak) UILabel *dateLabel;

@property (nonatomic,weak) UILabel *locationLabel;

@property (nonatomic,weak) UILabel *temperatureLabel;

@property (nonatomic,weak) UIImageView *weatherIcon;

@property (nonatomic,weak) UILabel *weatherLabel1;

@property (nonatomic,weak) UILabel *airQualityLabel;

@property (nonatomic,weak) UILabel *noticeLabel;

@property (nonatomic,strong) NSTimer *timer;

// 日期 地点 温度 天气图标 天气状况 空气质量
@end

@implementation WeatherView

- (instancetype)initWithFrame:(CGRect)frame cityName:(NSString *)cityName {
    
    if (self = [super initWithFrame:frame]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeCityName:) name:@"cityName" object:nil];

        [self displayScrollView];
        
        //定时器，是scrollview滚动
        [self displayLink];
    }
    return self;
}

-(NSMutableArray *)scrollArray {
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.delegate = self;
        _scrollView = scrollView;
        [self addSubview:scrollView];
        
        [scrollView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        UITapGestureRecognizer *cityNameTapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cityNameLabelTap:)];
        [scrollView addGestureRecognizer:cityNameTapRecognizer];
    }
    return _scrollView;
}

-(void)displayScrollView {

    [[[WeatherModel alloc]init] getWeatherWithCityName:@"上海" SuccessBlock:^(NSString *city, NSDictionary *currentWeather, NSString *pm25q) {
        
        [self getOneStringWithCity:city currentWeather:currentWeather pm25q:pm25q];
        
        NSString *weatherNotice = [self getOneStringWithCity:city currentWeather:currentWeather pm25q:pm25q];
        
        UILabel *noticelabel = [[UILabel alloc]init];
        noticelabel.text = weatherNotice;
        noticelabel.font = BOLDFONT(14);
        noticelabel.textColor = kThemeTextColor;
        [self.scrollView addSubview:noticelabel];
        self.noticeLabel = noticelabel;
        
        [noticelabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView.left).offset(5);
            make.centerY.equalTo(self.scrollView);
        }];
        
        [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(noticelabel.right).offset(sWidth);
        }];
        
    } errorBlock:nil];
    
    
    
}


-(NSArray *)provinceList {
    
    if (!_provinceList) {
        _provinceList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"province" ofType:@"plist"]];
        NSMutableArray *mArray = [NSMutableArray array];
        for (NSDictionary *dict in _provinceList) {
            ProvinceModel *model = [ProvinceModel provinceWithDictionary:dict];
            [mArray addObject:model];
        }
        _provinceList = mArray;
    }
    
    return _provinceList;
}

-(NSString *)getCurrentTime:(NSDate*)dateNow {
    
    if (!dateNow) {
        dateNow = [NSDate date];
    }
    
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    NSDateComponents *comps = [calendar components:unitFlags fromDate:dateNow];
    
    NSString *weekDay;
    
    switch ([comps weekday]) {
        case 1:
            weekDay = @"星期日";
            break;
        case 2:
            weekDay = @"星期一";
            break;
        case 3:
            weekDay = @"星期二";
            break;
        case 4:
            weekDay = @"星期三";
            break;
        case 5:
            weekDay = @"星期四";
            break;
        case 6:
            weekDay = @"星期五";
            break;
        case 7:
            weekDay = @"星期六";
            break;
            
    }
    
    NSString *dateStr = [NSString stringWithFormat:@"%ld年%ld月%ld日\t%@",[comps year],[comps month],[comps day],weekDay];
    
    return dateStr;
}

-(UILabel *)getWeatherLabelWithText:(NSString *)text {
    
    UILabel *label = [[UILabel alloc]init];
    label.text = text;
    label.font = BOLDFONT(13);
    label.textColor = kThemeTextColor;
    [self addSubview:label];
    
    return label;
}

-(void)cityNameLabelTap:(UITapGestureRecognizer *)sender {
    SSZProvinceViewController *pvc = [[SSZProvinceViewController alloc]init];
    
    if ([self.delegate respondsToSelector:@selector(jumpToProvinceTableView:)]) {
        [self.delegate jumpToProvinceTableView:pvc];
    }
}

-(void)changeCityName:(NSNotification *)notification {
    
    [[[WeatherModel alloc]init] getWeatherWithCityName:notification.userInfo[@"name"] SuccessBlock:^(NSString *city, NSDictionary *currentWeather, NSString *pm25q) {
        
        self.noticeLabel.text = [self getOneStringWithCity:city currentWeather:currentWeather pm25q:pm25q];
        
    } errorBlock:nil];
    
}

-(void)contentUpdateWithCity:(NSString *)city currentWeather:(NSDictionary *)currentWeather pm25q:(NSString *)pm25q {
    self.currentDateCity.text = [NSString stringWithFormat:@"%@\t\t%@",[self getCurrentTime:nil],city];
    
    NSInteger aq = [pm25q integerValue];
    NSString *aqStr;
    
    if (aq <= 50) {
        aqStr = @"优";
    } else if (aq <= 100) {
        aqStr = @"良";
    } else if (aq <= 150) {
        aqStr = @"轻度污染";
    } else if (aq <= 200) {
        aqStr = @"中度污染";
    } else {
        aqStr = @"重度污染";
    }
    
    self.temperatureAirQuality.text = [NSString stringWithFormat:@"温度:  %@\t空气质量:  %@",currentWeather[@"temperature"],aqStr];
    
    self.weatherLabel.text = currentWeather[@"weather"];
    
    [self.weatherIconView sd_setImageWithURL:currentWeather[@"dayPictureUrl"]];
}

-(NSString *)getOneStringWithCity:(NSString *)city currentWeather:(NSDictionary *)currentWeather pm25q:(NSString *)pm25q {
  
    NSInteger aq = [pm25q integerValue];
    NSString *aqStr;
    
    if (aq <= 50) {
        aqStr = @"优";
    } else if (aq <= 100) {
        aqStr = @"良";
    } else if (aq <= 150) {
        aqStr = @"轻度污染";
    } else if (aq <= 200) {
        aqStr = @"中度污染";
    } else {
        aqStr = @"重度污染";
    }

    return [NSString stringWithFormat:@"%@  ---  %@\t%@ \t温度: %@\t空气质量: %@",[self getCurrentTime:nil],city,currentWeather[@"weather"],currentWeather[@"temperature"],aqStr];;
}

//添加定时器
- (void)displayLink {
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(keepScrolling)];
    
    self.link = link;
    
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}


- (void)keepScrolling {
    
    self.offsetX += 0.3;
    
    [self.scrollView setContentOffset:CGPointMake(self.offsetX, 0)];
    
    if(self.offsetX > 485) {
        
        self.offsetX = -sWidth;
    }
    
}


-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end





















